from flask import render_template, request, Blueprint, jsonify, session

from service.login import send_message_code, get_cookies
from util.request_helper import get_parameter

login = Blueprint('login', __name__)


@login.route('/login')
def login_page():
    return render_template('login.html')


@login.route("/getCode")
def get_code():
    telephone = get_parameter(request, 'telephone')
    # 手机号存到session
    session['telephone'] = telephone
    with open("telephone.txt","w") as f:
        f.write(telephone)
    send_message_code(telephone)
    return jsonify({"status": "success"})


@login.route("/checkCookie")
def check_cookies():
    cookie = get_cookies()
    redirect_url = session.get("redirect_url")
    # session表明cookie的存在性从而决定是否进行页面拦截
    if cookie is not None:
        session['cookie'] = cookie
        return jsonify({
            "status": "success",
            "redirect_url": redirect_url
        })
    else:
        return jsonify({"status": "fail"})
